<template>
  <div class="container">
    <div
      :class="[
        'disapper-box',
        showBox == null ? '' : showBox ? 'box-show' : 'box-close',
      ]"
    >
      向上滑动消失
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      showBox: null,
    }
  },
  created() {},
  mounted() {
    //监听页面滚动
    window.addEventListener('scroll', this.scrollEvent) // 监听页面滚动
  },
  methods: {
    //页面滚动事件
    scrollEvent(e) {
      let scrollTop =
        window.pageYOffset ||
        document.documentElement.scrollTop ||
        document.body.scrollTop
      console.log(scrollTop)
      if (scrollTop >= 300) {
        this.showBox = false
        console.log('yincang ')
      } else {
        if (this.showBox !== null) this.showBox = true
      }
    },
  },
}
</script>

<style lang="less" scoped>
.container {
  display: flex;
  flex-direction: column;
  justify-content: flex-start;
  align-items: center;
  height: 150vh;
  overflow: hidden;
  user-select: none;
  background-image: linear-gradient(180deg, #7ccff1 61%, #0a8233 100%);
  .disapper-box {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 0.5rem;
    font-size: 0.15rem;
    text-align: center;
    line-height: 0.5rem;
    background-color: #fff;
  }
  .box-close {
    animation: box-close 0.5s forwards;
  }
  .box-show {
    animation: box-show 0.5s forwards;
  }
}
@keyframes box-show {
  0% {
    top: -0.5rem;
  }

  100% {
    top: 0;
  }
}
@keyframes box-close {
  0% {
    top: 0px;
  }
  100% {
    top: -0.5rem;
  }
}
</style>
